package com.shtel.paas.monitorcenter.asset.management.controller;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.shtel.paas.monitorcenter.asset.management.entity.AlarmRange;
import com.shtel.paas.monitorcenter.asset.management.entity.HostListEntity;
import com.shtel.paas.monitorcenter.asset.management.service.AlarmRangeService;
import com.shtel.paas.monitorcenter.asset.management.service.HostInfoService;
import com.shtel.paas.monitorcenter.util.Page;

@MapperScan("com.shtel.paas.monitorcenter.asset.management.mapper")
@RestController
public class HostListController{

	@Autowired
	HostInfoService hostInfoService;

	@Autowired
	AlarmRangeService alarmRangeService;
	

	@RequestMapping("/getFields")
	@ResponseBody
	public List<String> getFields(){
		List<String> fieldList = hostInfoService.getFields();
		
		return fieldList;
	}
	
	@RequestMapping("/exportHostList1")
	public ResponseEntity<byte[]> exportHostList1() {
		byte[] body = hostInfoService.exportHostList1();

		HttpHeaders headers = new HttpHeaders();// 设置响应头
		String fileName = null;
		try {
			fileName = new String("HostList.xls".getBytes(),"ISO8859-1");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}//防止中文乱码
		headers.add("Content-Disposition", "attachment;filename=" + fileName);
		HttpStatus statusCode = HttpStatus.OK;// 设置响应吗
		ResponseEntity<byte[]> response = new ResponseEntity<byte[]>(body, headers, statusCode);
		return response;
	}
	
	@RequestMapping("/exportHostList")
	@ResponseBody
	public String  exportHostList() {
		List<Map<String, Object>> body = hostInfoService.exportHostList();

		String s = JSONObject.toJSONString(body);
		return s;
	}
	

	/**
	 * 插入一条数据
	 * 
	 * @param param
	 * @return
	 */
	@RequestMapping("/insertHostInfo")
	public String insertHostInfo(@RequestBody MultiValueMap<String, String> param) {
		
		Map<String, String> params = param.toSingleValueMap();
		if(param.size()==0) {
			return "传入参数为空！";
		}
		
		hostInfoService.insertHostInfo(params);
		return "插入成功！";
	}
	
	/**
	 * 更新一条主机信息
	 * 
	 * @param param
	 */
	@RequestMapping("/updateHostInfo")
	public String updateHostInfo(@RequestBody MultiValueMap<String, String> param) {
		Map<String, String> params = param.toSingleValueMap();
		String id = params.get("id");
		if(null==id || "".equals(id)) {
			
			return "传入数据无ID";
		}
		
		hostInfoService.updateHostInfo(params);
		return "主机信息更新成功！";

	}

	/**
	 * 根据查询条件，查询主机信息
	 * 
	 * @param region
	 * @param ipAddr
	 * @param roomName
	 * @param rackName
	 * @return
	 */
	@RequestMapping(path = "/queryHostInfo")
	public Page<HostListEntity> queryHostInfo(@RequestParam("region") String region,
			@RequestParam("ipAddr") String ipAddr, @RequestParam("roomName") String roomName,
			@RequestParam("rackName") String rackName, @RequestParam("page") Integer page,
			@RequestParam("pageSize") Integer pageSize) {

		Page<HostListEntity> pageContent = 
				hostInfoService.quertHostInfo(region, ipAddr, roomName, rackName, page, pageSize);

		return pageContent;

	}

	/**
	 * 根据条件，删除主机信息
	 * 

	 * @return
	 */
	@RequestMapping("/deleteHostInfo")
	public String deleteHostInfo(@RequestParam("ids") String idstr) {
		List<Integer> ids = new ArrayList<Integer>();
		
		String[] idstrs = idstr.split(";");
		
		for(String s : idstrs) {
			ids.add(Integer.parseInt(s));
		}
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		if(ids.isEmpty()) {
			resultMap.put("desc", "传入ID为空！");
			resultMap.put("result", false);
			return "";
		}
		Integer num  = hostInfoService.daleteHostInfo(ids);
		resultMap.put("num", num);
		return "";
	}
	
	@RequestMapping("/queryAlarmConfig")
	public List<Map<String, Object>> queryAlarmConfig(String type, String ip, String kpiname) {
		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
		
		if(ip == null || "".equals(ip)) {
			return result;
		}
		
		if(kpiname == null || "".equals(kpiname)) {
			return result;
		}
		
		Map<String, String> param = new HashMap<String, String>();
		
		param.put("ip", ip);
		param.put("kpiname", kpiname);
		
		if("A".equals(type)) {
			List<AlarmRange> list = alarmRangeService.ruleRange(ip);
			result = hostInfoService.queryAlarmConfigType1(list,param);
		}else if("B".equals(type)) {
			result = hostInfoService.queryAlarmConfigType2(param);
		}else {
			
		}
		
		return result;
	}
	
}
